package ru.rchervot.sanasar.client.view;

import com.google.gwt.dom.client.Document;
import com.google.gwt.dom.client.Element;
import com.google.gwt.event.dom.client.DoubleClickEvent;
import com.google.gwt.event.dom.client.DoubleClickHandler;
import com.google.gwt.event.dom.client.HasDoubleClickHandlers;
import com.google.gwt.event.shared.HandlerRegistration;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.Grid;
import com.google.gwt.user.client.ui.HasVerticalAlignment;
import com.google.gwt.user.client.ui.Label;

public abstract class AbstractResultsHeaderView extends Composite
implements ResultsHeaderPresenter.Display, HasDoubleClickHandlers {
	
	protected final Grid basePanel = new Grid(1,1);
	Label resultsInfo = new Label("Results");
	
//	protected ActionMessagesWidget actionMessages = new ActionMessagesWidget(); 
	
	private ResultsHeaderPresenter presenter;
	
	public AbstractResultsHeaderView() {
		super();

		resultsInfo.setStylePrimaryName("AbstractResultsHeaderView_Title");
		
		basePanel.setCellPadding(0);
		basePanel.setCellSpacing(0);
		basePanel.getCellFormatter().setVerticalAlignment(0, 0, 
				HasVerticalAlignment.ALIGN_MIDDLE);
		basePanel.setStylePrimaryName("AbstractResultsHeaderView");
		basePanel.setWidth("100%");
		
		basePanel.setWidget(0,0, resultsInfo);
		basePanel.setHeight(Constants.kResultsHeaderHeight + "px");
//		
//		this.ensureDebugId("AbstractResultsHeaderView");
//		
//		Element el = Document.get().getElementById("AbstractResultsHeaderView");
//		
//		el.setAttribute("onselectstart", "function() { return false; }");
		
		this.initWidget(basePanel);
	}
	
	@Override
	protected void onLoad() {
		super.onLoad();
		
//		preventTextSelectOnDblClick();
	}
	
	public static native void preventTextSelectOnDblClick() /*-{
  	header = $doc.getElementById('AbstractResultsHeaderView');
  	
  	$wnd.alert("headeris null? " + (header == null ? "t" : "f"));
  	
  	header.onselectstart = function()
		{
    	return false;
		};
		
		header.onmousedown = function()
		{
    	return false;
		};
  	
	}-*/;

	@Override
	public void setPresenter(ResultsHeaderPresenter presenter) {
		this.presenter = presenter;
	}
	
	@Override
	public void setHeaderTitle(String s) {
		resultsInfo.setText(s);
	}
	
	@Override
	public HasDoubleClickHandlers getBasePanel() {
		return this;
	}
	
	@Override
	public HandlerRegistration addDoubleClickHandler(DoubleClickHandler handler) {
		return this.addDomHandler(handler, DoubleClickEvent.getType());
	}
	
}
